package com.health.controller;


import com.health.pojo.User;
import com.health.service.UserService;
import com.health.vo.Result;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.UUID;

@RestController
public class LoginController {
    @Resource
    private UserService userService;


    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User userLogin = userService.findUserByLogin(user);
        if (userLogin != null) {
            //登录成功生成Token,存储到Redis中
            String token = "LOGIN_USER:" + UUID.randomUUID();
            redisTemplate.opsForValue().set(token, userLogin, Duration.ofMinutes(30));

            return new Result(true, "登录成功", token);
        }

        return new Result(false, "用户名或密码输入错误");
    }
}